package sdk.webview.fmc.com.fmcsdk.h5;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.TextView;

import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;

import sdk.webview.fmc.com.fmcsdk.util.NetworkUtils;
import sdk.webview.fmc.com.fmcsdk.util.XLog;


/**
 * X5webView的基本配置
 * Created by ydm on 2018/1/3.
 */

public class X5WebView extends WebView {
    TextView title;
    private Context context;

    @SuppressLint("SetJavaScriptEnabled")
    public X5WebView(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
        this.context = arg0;
        // this.setWebChromeClient(chromeClient);
        // WebStorage webStorage = WebStorage.getInstance();
        initWebViewSettings();
        this.getView().setClickable(true);
    }

    private void initWebViewSettings() {


        WebSettings webSetting = this.getSettings();
        webSetting.setJavaScriptEnabled(true);
        String appCachePath = getContext().getCacheDir().getAbsolutePath();
        webSetting.setAppCachePath(appCachePath);
        webSetting.setAppCacheEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(false);
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(true);
        webSetting.setSavePassword(false);
        // webSetting.setLoadWithOverviewMode(true);
        webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setBlockNetworkImage(true);

        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        //如果有网络获取网络数据，否则加载本地的缓存
        // webSetting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        //  webSetting.setCacheMode(WebSettings.LOAD_DEFAULT);
        if (NetworkUtils.isAvailable(context)) {
//            XLog.i("WebSettings.LOAD_DEFAULT");
//            if (isAppDataRestricted(context)) {
//                webSetting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//            } else {
                webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
//            }
        } else {
            webSetting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            XLog.i("WebSettings.LOAD_CACHE_ELSE_NETWORK");
        }

    }

    public static boolean isAppDataRestricted(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager != null) {
                int restrictBackgroundStatus = connectivityManager.getRestrictBackgroundStatus();
                return restrictBackgroundStatus != ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED;
            }
        }
        return false;
    }
}
